package com.ccgk.logistics.controller;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Company;
import com.ccgk.logistics.service.CompanyService;
import com.ccgk.logistics.vo.CompanyTypeData;
import com.ccgk.logistics.vo.CompanyTypeData.CompanyTypeVO;

@Controller
@RequestMapping("/company")
public class CompanyController {
	@Autowired
	private CompanyService companyService;
	
	/**查询公司列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		String name = request.getParameter("name");
		CompanyService.CompanyCriteria  criteria = new CompanyService.CompanyCriteria();
		if(StringUtils.isNotBlank(name))
			criteria.nameLike(name);
		PaginationVo<Company> result = companyService.findList(criteria, currentPage, pageSize);
		
		HttpSession session = request.getSession();
	
		String filepath = session.getServletContext().getRealPath("static")+"\\xml\\CompanyType.xml";
		//InputStream input = this.getClass().getResourceAsStream("/CompanyType.xml");
		
		CompanyTypeData companyType = new CompanyTypeData(filepath);
		List<CompanyTypeVO> companyTypeList = companyType.getListData();
		
		for(Company company:result.getData()){
			String value = companyType.getValue(company.getType());
			company.setType(value);
		}
		
		request.setAttribute("result",result);
		return "company/list";
	}
	
	/**获取单个公司*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model,HttpServletRequest request){
		Company company = companyService.getById(id);
		model.addAttribute("obj", company);
		
		List<CompanyTypeVO> companyTypeList = CompanyTypeData.getListData();
		request.setAttribute("companyTypeList",companyTypeList);
		return "company/edit";
	}
	
	/**更新公司信息*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Company company){
		Company _company = companyService.getById(company.getId());
		if(_company != null && !_company.getId().equals(company.getId()))
			return "{flag:1}";
		companyService.update(company);
		return "{flag:0}";
	}
	/**新增公司页*/
	@RequestMapping("/add")
	public String showAdd(HttpServletRequest request){
		List<CompanyTypeVO> companyTypeList = CompanyTypeData.getListData();
		request.setAttribute("companyTypeList",companyTypeList);
		return "company/add";
	}
	/**新增公司*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Company company,HttpServletResponse response){
		if(companyService.getByName(company.getName()) != null){
			return "{flag:1}";
		}
		companyService.add(company);
		return "{flag:0}";
	}
	/**删除公司*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		companyService.batchDelete(idIntArray);
		return "{success:true}";
	}
	
	@ResponseBody
	@RequestMapping("/ajaxData")
	public List<Company> ajaxData(HttpServletRequest request){
		String name = request.getParameter("name");
		List<Company> result = companyService.findAll();
		request.setAttribute("result",result);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/ajaxDataById")
	public Company ajaxDataById(HttpServletRequest request){
		String id = request.getParameter("id");
		Company company = companyService.getById(Integer.parseInt(id));
		return company;
	}
}
